const data = require("../res/pdf_dice_data.json");

const fs = require("fs");

const main = () => {
    const DICE_COUNT = 100;
    const SIMULATION_ROUNDS = 100000;

    const current_enhance = {
        character: 0,
        weapon: 0,
    }
    const enhancement = {
        character:  [0.4, 0.2, 0.1, 0.2, 0.5, 0.8],
        weapon:     [0.5, 0.3, 0.3, 0.3, 0.2]
    }
    const pool = {
        character: 80,
        weapon: 0,
    }

    const CHARA_LEN = 90;
    const WEAPON_LEN = 80;
    const pdf = {
        character: data.character1.probability,
        weapon: data.weapon.probability
    }

    let best_path = {
        path: [],
        value: 0
    }

    let all_path = paths(current_enhance);
    
    for(let index in all_path) {
        let {path, len} = all_path[index];
        let sum = 0;
        console.log(`${index*1+1}/${all_path.length}`);

        for (let round_id = 0; round_id < SIMULATION_ROUNDS; round_id++) {
            let path_index = 0;
            let dice_index = pool;
            let result = {
                character: 0,
                weapon: 0,
            }
            for (let i = 0; i < DICE_COUNT; i++) {
                if(path_index >= len) break;
                let pool_name = path[path_index];
                if(dice(pdf[pool_name], dice_index[pool_name])) {
                    dice_index[pool_name] = 0;
                    path_index++;
                    result[pool_name]++;
                    continue
                }
                dice_index[pool_name]++;
            }
            
            const v = value(result, current_enhance, enhancement);
            sum += v;
        }

        if(sum / SIMULATION_ROUNDS > best_path.value) {
            best_path.path = path;
            best_path.value = sum / SIMULATION_ROUNDS;
        }
    }

    console.log(best_path)
}

const paths = (current_enhancement) => {
    const MAX_CHARACTER_ENHANCEMENT = 6;
    const MAX_WEAPON_ENHANCEMENT = 5;

    aim = {
        character: MAX_CHARACTER_ENHANCEMENT - current_enhancement.character,
        weapon: MAX_WEAPON_ENHANCEMENT - current_enhancement.weapon,
    }

    len = aim.character + aim.weapon;
    ret = [];

    for (let i = 0; i < Math.pow(2, len); i++) {
        // 0 -> chara, 1 -> weapon
        let path = new Array(len);
        for(let j = 0; j < len; j++) {
            if ((i >> j) & 1) {
                path[j] = "weapon";
            } else {
                path[j] = "character";
            }
        }
        ret.push({path, len})
    }

    return ret;
}

const dice = (p, index) => {
    if(index >= p.length) return false;
    return Math.random() < p[index];
}

const value = (result, current_enhance, enhancement) => {
    // console.log(result)
    let total_character = result.character + current_enhance.character;
    total_character = total_character > enhancement.character.length ? enhancement.character.length : total_character;

    let total_weapon = result.weapon + current_enhance.weapon;
    total_weapon = total_weapon > enhancement.weapon.length ? enhancement.weapon.length : total_weapon;

    let e = {
        character: enhancement.character.slice(current_enhance.character, total_character),
        weapon: enhancement.weapon.slice(current_enhance.weapon, total_weapon),
    }
    let v = 1;
    for(let key of ["character", "weapon"]) {
        for(let element of e[key]) {
            v *= 1 + element;
        }
    }
    return v - 1
}

main()